package com.app.lxx.pregnancyberth.base;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import androidx.annotation.Nullable;

import android.util.Log;

import com.app.lxx.pregnancyberth.BuildConfig;


/**
 * Created by Lxx on StudentsReading
 * 2020/8/27 09:27 星期四
 * Details:
 */
public class ANRService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("ANRService", "onCreate ANRService");
        startForeground(0, new Notification());
        exception();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }

    private int lasttick, mTick;//两次计数器的值
    private Handler mHandler = new Handler();
    private boolean flag = true;

    private void exception() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag) {
                    lasttick = mTick;
                    mHandler.post(tickerRunnable);//向主线程发送消息 计数器值+1
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (mTick == lasttick) {
                        flag = false;
                        Log.e("ANRService", "anr happned in here");
                        handleAnrError();
                    }
                }
            }
        }).start();
    }

    //发生anr的时候，在此处写逻辑
    private void handleAnrError() {
        Log.e("ANRService", "发生 ANR");
        if (BuildConfig.DEBUG) {
            return;
        }
        restartApp();
    }

    private final Runnable tickerRunnable = new Runnable() {
        @Override
        public void run() {
            mTick = (mTick + 1) % 10;
        }
    };

    // 重启应用
    @SuppressLint("WrongConstant")
    public void restartApp() {
        try {
            Intent intent = new Intent();
            // 参数1：包名，参数2：程序入口的activity
            intent.setClassName(getPackageName(), "com.app.lxx.pregnancyberth.ui.MainActivity");
            PendingIntent restartIntent = PendingIntent.getActivity(
                    getApplicationContext(), 0, intent,
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用

            ActivityManger.finshApp();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(10);
        } catch (Exception e) {

        }

    }

}
